package common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Stack;

import javax.xml.parsers.SAXParser;   
import javax.xml.parsers.SAXParserFactory;   
  
import org.xml.sax.Attributes;   
import org.xml.sax.InputSource;   
import org.xml.sax.helpers.DefaultHandler; 

public class XMLSAXAnswerHandler extends DefaultHandler { // implements XMLParse

	private boolean isReady = false;
	private String tag = null;
	private int testID;
	private String extPath;
	private int number=0;
	public int getNumber() {
		return number;
	}
	
	public XMLSAXAnswerHandler(int testID, String extPath) {
		super();
		this.testID=testID;
		this.extPath=extPath;
	}

	public void characters(char ch[], int start, int length) {
		if (isReady&&tag.equals("in")){
			File inputXmlFile=new File(extPath+"input");
			if(!inputXmlFile.exists()){
				inputXmlFile.mkdirs();
			}
			FileWriter fWriter=null;
			PrintWriter pWriter=null;
			try {
				fWriter=new FileWriter(new File(extPath+"input/"+testID+"-"+number+".txt"),true);
				pWriter=new PrintWriter(fWriter);
				pWriter.println(new String(ch, start, length));
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(pWriter!=null){
				pWriter.flush();
				pWriter.close();
				}
				if(fWriter!=null){
					try {
						fWriter.flush();
						fWriter.close();
					} catch (IOException e) {
						fWriter=null;
					}
				}
			}
		}
		if (isReady&&tag.equals("out")){
			File inputXmlFile=new File(extPath+"output");
			if(!inputXmlFile.exists()){
				inputXmlFile.mkdirs();
			}
			FileWriter fWriter=null;
			PrintWriter pWriter=null;
			try {
				fWriter=new FileWriter(new File(extPath+"output/"+testID+"-"+number+".txt"),true);
				pWriter=new PrintWriter(fWriter);
				pWriter.println(new String(ch, start, length));
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(pWriter!=null){
				pWriter.flush();
				pWriter.close();
				}
				if(fWriter!=null){
					try {
						fWriter.flush();
						fWriter.close();
					} catch (IOException e) {
						fWriter=null;
					}
				}
			}
		}
	}

	public void startElement(String uri, String localName, String name,
			Attributes attrs) {
		if(name.equals("case")){
			number++;
		}
		if (name.equals("in")) {
			tag = "in";
		} else if (name.equals("out")) {
			tag = "out";
		} else if (name.equals("line")) {
			isReady = true;
		} 
	}
	
	public void endElement(String uri, String localName, String name){
		if (name.equals("line")) {
			isReady = false;
		} else if(name.equals("cases")){
			close();
		}
	}
	
	private void close() {
		
	}
	public static void main(String args[]) {   
		  try {   
		    SAXParserFactory sf = SAXParserFactory.newInstance();   
		    SAXParser sp = sf.newSAXParser();   
		    XMLSAXAnswerHandler reader = new XMLSAXAnswerHandler(1,"C://");   
		    sp.parse(new InputSource("C://xmlCase.xml"), reader);   
		   } catch (Exception e) {   
		    e.printStackTrace();   
		   }   
		}   

}
